Imports System.ComponentModel
Imports DotNetNuke.UI.WebControls
Imports Aricie.DNN.UI.WebControls.EditControls


Namespace Aricie.DNN.Services.Url.FriendlyUrl

    <Serializable()> _
    Public Class UrlParam
        Implements IEquatable(Of UrlParam)
        Implements IEqualityComparer(Of UrlParam)


        Private Shared _UrlParamsByParams As New Dictionary(Of String, UrlParam)(StringComparer.OrdinalIgnoreCase)

        'Private _Any As Boolean = False
        Private _SyntaxOptions As List(Of String)
        Private _HashCode As Integer = -1

        <Editor(GetType(ListEditControl), GetType(EditControl))> _
        <LabelMode(LabelMode.None)> _
        Public ReadOnly Property SyntaxOptions() As List(Of String)
            Get
                Return _SyntaxOptions
            End Get
        End Property



        Public Shared Function FromSyntax(ByVal ParamArray syntaxOptions() As String) As UrlParam
            Dim toReturn As UrlParam = Nothing
            Dim changed As Boolean = False
            For i As Integer = 0 To syntaxOptions.Length - 1
                'syntaxOptions(i) = syntaxOptions(i).ToLowerInvariant
                If Not _UrlParamsByParams.TryGetValue(syntaxOptions(i), toReturn) Then
                    changed = True
                End If
            Next
            If toReturn Is Nothing Then
                toReturn = New UrlParam(syntaxOptions)
            ElseIf changed Then
                For Each syntaxOption As String In syntaxOptions
                    If Not toReturn.Match(syntaxOption) Then
                        toReturn.AddSyntax(syntaxOption)
                    End If
                Next
            End If

            Return toReturn
        End Function


        Friend Sub New()

        End Sub

        Private Sub AddSyntax(ByVal syntax As String)
            Me._SyntaxOptions.Add(syntax)
            UrlParam._UrlParamsByParams(syntax) = Me
        End Sub

        Private Sub New(ByVal ParamArray syntaxOptions() As String)

            Me._SyntaxOptions = New List(Of String)(syntaxOptions.Length)
            For Each syntaxOption As String In syntaxOptions
                Me.AddSyntax(syntaxOption)
            Next
        End Sub


        Private Function Match(ByVal loweredParamName As String) As Boolean
            Dim syntaxMatch As UrlParam = Nothing
            Return UrlParam._UrlParamsByParams.TryGetValue(loweredParamName, syntaxMatch) AndAlso syntaxMatch Is Me
        End Function

        Public Overrides Function GetHashCode() As Integer
            If Me._HashCode = -1 Then
                Me._HashCode = Me.SyntaxOptions(0).GetHashCode
            End If
            Return Me._HashCode
        End Function

        Public Overrides Function Equals(ByVal value As Object) As Boolean
            Return TypeOf value Is UrlParam AndAlso Me.Equals1(DirectCast(value, UrlParam))
        End Function

        Public Function Equals1(ByVal other As UrlParam) As Boolean Implements IEquatable(Of UrlParam).Equals
            Return other Is Me
        End Function


        Public Shared DNNParams As New UrlParam("DnnParams")
        Public Shared QueryString As New UrlParam("QueryString")
        Public Shared ModuleId As New UrlParam("mid", "moduleid")
        Public Shared UserId As New UrlParam("userid", "uid")
        Public Shared PortalId As New UrlParam("portalid")
        Public Shared Pid As New UrlParam("pid")

        Public Shared TabId As New UrlParam("tabid")
        Public Shared ControlKey As New UrlParam("ctl")
        Public Shared Language As New UrlParam("language")


        Public Function Equals2(ByVal x As UrlParam, ByVal y As UrlParam) As Boolean _
            Implements IEqualityComparer(Of UrlParam).Equals
            Return x.Equals1(y)
        End Function

        Public Function GetHashCode1(ByVal obj As UrlParam) As Integer _
            Implements IEqualityComparer(Of UrlParam).GetHashCode
            Return obj.GetHashCode
        End Function

        Public Shared Operator =(ByVal leftOperand As UrlParam, ByVal rightOperand As UrlParam) As Boolean
            Return leftOperand.Equals1(rightOperand)
        End Operator

        Public Shared Operator <>(ByVal leftOperand As UrlParam, ByVal rightOperand As UrlParam) As Boolean
            Return Not leftOperand.Equals1(rightOperand)
        End Operator

        Public Overrides Function ToString() As String
            Return me.SyntaxOptions(0)
        End Function
    End Class
End Namespace
